home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / ODBC for Macintosh / ODBC Tools / SampleSetup / Sources / DBMSsetup.c next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  6.4 KB  |  353 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    DBMSsetup.c
  3.  *
  4.  *    Data source setup dialog, and its associated functions.
  5.  *
  6.  *    (c) Apple Computer, Inc 1993
  7.  */
  8.  
  9. #include <Controls.h>
  10. #include <Dialogs.h>
  11. #include <Events.h>
  12. #include <Lists.h>
  13. #include <Menus.h>
  14. #include <OSUtils.h>
  15. #include <Quickdraw.h>
  16. #include <String.h>
  17. #include <Strings.h>
  18. #include <TextEdit.h>
  19. #include <Types.h>
  20. #include <Windows.h>
  21.  
  22. #include "DialogUtilities.h"
  23. #include "ODBCINST.H"
  24. #include "ODBCdbmsSetup.h"
  25.  
  26. #include "DBMSsetup.h"
  27.  
  28. /*
  29.  *    Defines
  30.  */
  31.  
  32. #define kDBMSsetupDlogID            134  
  33.  
  34. #define iOKButton                    1 
  35. #define iCancelButton                2 
  36. #define iDataSourceNameLabel        3 
  37. #define iDescriptionLabel            4 
  38. #define iOtherLabel                    5 
  39. #define iDataSourceName                6 
  40. #define iDescription                7 
  41. #define iOther                        8 
  42. #define iTranslationLabel            9 
  43. #define iTranslationName            10 
  44. #define iSelectButton                11 
  45. #define iLine1                        12 
  46. #define iLine2                        13 
  47.  
  48. /*
  49.  *    Structures
  50.  */
  51.  
  52. typedef struct
  53. {
  54.     GrafPtr            savePort;
  55.     DialogPtr        dialog;
  56.     DataSourceInfo    *info;
  57. }
  58. DoDBMSsetupRec;
  59.  
  60. /*
  61.  *    Globals
  62.  */
  63.  
  64. static DoDBMSsetupRec    gDoDBMSsetupRec;
  65. static char                gTranslate[256];
  66. static char                gTranslateOption[256];
  67.  
  68. /*
  69.  *    Prototypes
  70.  */
  71.  
  72. Boolean            Initialize            (DataSourceInfo *info);
  73. void            Cleanup                ();
  74. Boolean            DoOkButton            ();
  75. void            DoDataSourceName    ();
  76. void            DoDescription        ();
  77. void            DoOther                ();
  78. void            DoSelectButton        ();
  79. void            AdjustItems            ();
  80. Boolean            GetTranslator        (DialogPtr dialog, char *dataSourceName, char *name, long nameMax, long *nameSize, DWORD* option);
  81. void            NumToHexString        (long num, char *ptr, long n);
  82. void            HexStringToNum        (char *ptr, long *num);
  83.  
  84. /*
  85.  *    Public functions
  86.  */
  87.  
  88. Boolean
  89. DoDBMSsetup(WindowPtr parentWindow, DataSourceInfo *info)
  90. {
  91. #pragma unused ( parentWindow )
  92.     /*
  93.      *    Display the data source setup dialog, centered over parentWindow, and
  94.      *    handle events until the user dismisses the dialog.
  95.      */
  96.     
  97.     Boolean        result;
  98.     Boolean        done = false;
  99.     short        item;
  100.     
  101.     if (!Initialize(info)) return false;
  102.     
  103.     while (!done)
  104.     {
  105.         MovableDialog((ModalFilterProcPtr) StandardFilter, &item);
  106.         switch (item)
  107.         {
  108.             case iOKButton:
  109.                 done = result = DoOkButton();
  110.                 break;
  111.             
  112.             case iCancelButton:
  113.                 result = false;
  114.                 done = true;
  115.                 break;
  116.             
  117.             case iDataSourceName:
  118.                 DoDataSourceName();
  119.                 break;
  120.             
  121.             case iDescription:
  122.                 DoDescription();
  123.                 break;
  124.             
  125.             case iOther:
  126.                 DoOther();
  127.                 break;
  128.             
  129.             case iSelectButton:
  130.                 DoSelectButton();
  131.                 break;
  132.         }
  133.     }
  134.     
  135.     Cleanup();
  136.     
  137.     return result;
  138. }
  139.  
  140. /*
  141.  *    Private functions
  142.  */
  143.  
  144. static Boolean
  145. Initialize(DataSourceInfo *info)
  146. {
  147.     /*
  148.      *    Create the dialog and initialize its items - return false if anything fails.
  149.      */
  150.     
  151.     DialogPtr    dialog = NULL;
  152.     GrafPtr        savePort = NULL;
  153.     
  154.     GetPort(&savePort);
  155.     InitCursor();
  156.     dialog = GetNewDialog(kDBMSsetupDlogID, nil, (WindowPtr) -1L);
  157.     if (dialog == NULL) goto bail;
  158.     SetPort(dialog);
  159.     
  160.     SetDUserItem(dialog, iLine1, (ProcPtr) DrawItemLineGray);
  161.     SetDUserItem(dialog, iLine2, (ProcPtr) DrawItemLineGray);
  162.     
  163.     c2pstr(info->name);
  164.     SetDText(dialog, iDataSourceName, info->name);
  165.     SetWTitle((WindowPtr) dialog, info->name);
  166.     p2cstr(info->name);
  167.     
  168.     c2pstr(info->description);
  169.     SetDText(dialog, iDescription, info->description);
  170.     p2cstr(info->description);
  171.     
  172.     c2pstr(info->other);
  173.     SetDText(dialog, iOther, info->other);
  174.     p2cstr(info->other);
  175.     
  176.     strcpy(gTranslate, info->translate);
  177.     strcpy(gTranslateOption, info->translateOption);
  178.     
  179.     gDoDBMSsetupRec.savePort = savePort;
  180.     gDoDBMSsetupRec.dialog = dialog;
  181.     gDoDBMSsetupRec.info = info;
  182.     
  183.     AdjustItems();
  184.     ShowWindow(dialog);
  185.     
  186.     return true;
  187.     
  188. bail:
  189.     
  190.     if (dialog != NULL) DisposDialog(dialog);
  191.     SetPort(savePort);
  192.     
  193.     return false;
  194. }
  195.  
  196. static void
  197. Cleanup()
  198. {
  199.     /*
  200.      * Dispose of the dialog and its assundry allocations
  201.      */
  202.     
  203.     DisposDialog(gDoDBMSsetupRec.dialog);
  204.     SetPort(gDoDBMSsetupRec.savePort);
  205. }
  206.  
  207. static Boolean
  208. DoOkButton()
  209. {
  210.     /*
  211.      *    Copy the dialog settings to the DataSourceInfo structure
  212.      */
  213.     
  214.     GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, gDoDBMSsetupRec.info->name);
  215.     p2cstr(gDoDBMSsetupRec.info->name);
  216.     
  217.     GetDText(gDoDBMSsetupRec.dialog, iDescription, gDoDBMSsetupRec.info->description);
  218.     p2cstr(gDoDBMSsetupRec.info->description);
  219.     
  220.     GetDText(gDoDBMSsetupRec.dialog, iOther, gDoDBMSsetupRec.info->other);
  221.     p2cstr(gDoDBMSsetupRec.info->other);
  222.     
  223.     strcpy(gDoDBMSsetupRec.info->translate, gTranslate);
  224.     strcpy(gDoDBMSsetupRec.info->translateOption, gTranslateOption);
  225.     
  226.     return true;
  227. }
  228.  
  229. static void
  230. DoDataSourceName()
  231. {
  232.     /*
  233.      *    Handle changes to the "name" edit field
  234.      */
  235.     
  236.     AdjustItems();
  237. }
  238.  
  239. static void
  240. DoDescription()
  241. {
  242.     /*
  243.      *    Handle changes to the "description" edit field
  244.      */
  245.     
  246.     AdjustItems();
  247. }
  248.  
  249. static void
  250. DoOther()
  251. {
  252.     /*
  253.      *    Handle changes to the "other" edit field
  254.      */
  255.     
  256.     ;
  257. }
  258.  
  259. static void
  260. DoSelectButton()
  261. {
  262.     /*
  263.      *    Select a translator
  264.      */
  265.     
  266.     char    translate[256];
  267.     long    len;
  268.     DWORD    option;
  269.     long    optionLong;
  270.     Boolean    success;
  271.     
  272.     HexStringToNum(gTranslateOption, &optionLong);
  273.     option = (DWORD) optionLong;
  274.     
  275.     HideWindow(gDoDBMSsetupRec.dialog);
  276.     success = GetTranslator(gDoDBMSsetupRec.dialog, gDoDBMSsetupRec.info->name, translate, 256, &len, &option);
  277.     ShowWindow(gDoDBMSsetupRec.dialog);
  278.     
  279.     if (success)
  280.     {
  281.         strcpy(gTranslate, translate);
  282.         NumToHexString(option, gTranslateOption, 8);
  283.         
  284.         AdjustItems();
  285.     }
  286. }
  287.  
  288. static void
  289. AdjustItems()
  290. {
  291.     /*
  292.      *    Enable/disable dialog items as appropriate
  293.      */
  294.     
  295.     char    name[256];
  296.     
  297.     GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, name);
  298.     EnableDItem(gDoDBMSsetupRec.dialog, iOKButton, (name[0] > 0));
  299.     
  300.     c2pstr(gTranslate);
  301.     SetDText(gDoDBMSsetupRec.dialog, iTranslationName, gTranslate);
  302.     p2cstr(gTranslate);
  303. }
  304.  
  305. static Boolean
  306. GetTranslator(DialogPtr dialog, char *dataSourceName, char *name, long nameMax, long *nameSize, DWORD* option)
  307. {
  308.     /*
  309.      *    Get the translator name and option value. Write them out to the ODBC prefs file.
  310.      */
  311.     
  312.     Boolean    success;
  313.     UINT    size;
  314.     
  315.     size = nameSize;
  316.     success = (Boolean) SQLGetTranslator(dialog, name, nameMax, &size, NULL, 0, NULL, option);
  317.     *nameSize = size;
  318.     
  319.     return success;
  320. }
  321.  
  322. static void
  323. NumToHexString(long num, char *str, long n)
  324. {
  325.     char *digits = "0123456789ABCDEF";
  326.     
  327.     while (n-- > 0)
  328.         *str++ = digits[(num >> (4*n)) & 0x000F];
  329.     
  330.     *str = '\0';
  331. }
  332.  
  333. static void
  334. HexStringToNum(char *str, long *num)
  335. {
  336.     char *p = str, ch;
  337.     long v = 0;
  338.     
  339.     while (*p == ' ' || *p == '\t') p++;
  340.     
  341.     for (;;)
  342.     {
  343.         if (! (ch = *p++)) { p--; break; }
  344.         
  345.             if (ch >= '0' && ch <= '9') { v <<= 4; v += ch - '0'; }
  346.         else if (ch >= 'a' && ch <= 'f') { v <<= 4; v += ch - 'a' + 10; }
  347.         else if (ch >= 'A' && ch <= 'F') { v <<= 4; v += ch - 'A' + 10; }
  348.         else break;
  349.     }
  350.     
  351.     *num = v;
  352. }
  353.